﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Configuration;

namespace DSLOrder
{
    class Api
    {
        public string TokenUrl = ConfigurationManager.AppSettings["TokenUrl"].ToString();
        public string GetOrderUrl = ConfigurationManager.AppSettings["GetOrderUrl"].ToString();
        public string SendOrderUrl = ConfigurationManager.AppSettings["SendOrderUrl"].ToString();
        public string user = ConfigurationManager.AppSettings["user"].ToString();
        public string pass = ConfigurationManager.AppSettings["pass"].ToString();
        public string uid = ConfigurationManager.AppSettings["uid"].ToString();

        Common Init = new Common();

        public string Token = "";
        //获取Token
        public string GetToKen()
        {
            string requestData="{\"userId\":\""+user+"\",\"userPd\":\""+pass+"\"}";
            string result = sendPost(TokenUrl, requestData);
            return result;
        }
        //获取报价单
        public string GetOrder()
        {
            string requestData = "{\"id\":\"" + uid + "\"}";
            string result = sendPost(GetOrderUrl, requestData);
            return result;
        }
        //上传报价单
        public string UpOrder(string requestData)
        {
            string result = sendPost(SendOrderUrl, requestData);
            return result;
        }
        private string sendPost(string url, string  param)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 50; 
            string result = "";
            byte[] byteData = Encoding.UTF8.GetBytes(param);
            try
            {
               // System.Net.ServicePointManager.DefaultConnectionLimit = 50;
               // System.GC.Collect();
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ServicePoint.Expect100Continue = false;//必须添加 否则一直超时
                request.ProtocolVersion = HttpVersion.Version11;
                request.Method = "POST";
                //request.ContentType = "application/x-www-form-urlencoded";
                request.ContentType = "application/json";
                request.ContentLength = byteData.Length;                
                request.Referer = url;                
                request.Accept = "*/*";
                request.Timeout = 180 * 10000;
                //request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

                request.UserAgent = "Mozilla-Firefox-Spider(Wenanry)";                 
                if (!string.IsNullOrEmpty(Token))
                {
                    Init.WriteLog(Token.ToString(), false);
                    request.Headers.Add("Token", Token);
                }
                //request.KeepAlive = false;
                //request.UseDefaultCredentials = true;
                //request.ServicePoint.Expect100Continue = false;

                Stream stream = request.GetRequestStream();
                stream.Write(byteData, 0, byteData.Length);
                stream.Flush();
                stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream backStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(backStream, Encoding.GetEncoding("UTF-8"));
                result = sr.ReadToEnd();
                sr.Close();
                backStream.Close();
                response.Close();
                request.Abort();
            }
            catch (WebException ex)
            {
                Init.WriteLog(ex.ToString(), true);
                result = ex.Message;
            }
            return result;
        }
    }
}
